<template>
  <div class="register-container">
    <div class="inner-container">
      <el-form ref="ruleFormRef" :model="form" :rules="rules" label-position="top" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm_password">
          <el-input type="password" v-model="form.confirm_password"></el-input>
        </el-form-item>
        <el-form-item label="考试类型" prop="exam_type">
          <el-radio-group v-model="form.exam_type">
            <el-radio value="1">GMART</el-radio>
            <el-radio value="2">GRE</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="计划考试时间" prop="plan_time">
          <el-row class="exam-time-row" style="width: 100%">
            <el-col :span="6">
              <el-select v-model="form.plan_time.year" placeholder="选择年">
                <el-option v-for="year in years" :key="year" :label="year" :value="year"></el-option>
              </el-select>
            </el-col>
            <el-col :span="2">
              <div class="exam-time-divider">年——</div>
            </el-col>
            <el-col :span="6">
              <el-select v-model="form.plan_time.month" placeholder="选择月">
                <el-option v-for="month in months" :key="month" :label="month" :value="month"></el-option>
              </el-select>
            </el-col>
            <el-col :span="2">
              <div class="exam-time-divider">月——</div>
            </el-col>
            <el-col :span="7">
              <el-select v-model="form.plan_time.day" placeholder="选择日">
                <el-option v-for="day in days" :key="day" :label="day" :value="day"></el-option>
              </el-select>
            </el-col>
            <el-col :span="1">
              <div>日</div>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="备考规划（免费）" prop="plan">
          <el-radio-group v-model="form.plan">
            <el-radio :value="1">需要</el-radio>
            <el-radio :value="2">不需要</el-radio>
          </el-radio-group>
        </el-form-item>
        <template v-if="form.plan == 1">
          <div class="container-exam">
            <el-form-item label="微信号" prop="weixin">
              <el-input v-model="form.weixin"></el-input>
            </el-form-item>
            <el-form-item label="英语基础（托福/雅思/托业 总分）" prop="total_score">
              <el-input v-model="form.total_score"></el-input>
            </el-form-item>
            <el-form-item label="阅读小分" prop="read_score">
              <el-input v-model="form.read_score"></el-input>
            </el-form-item>
            <el-form-item label="高考数据成绩" prop="maths_score">
              <el-input v-model="form.maths_score"></el-input>
            </el-form-item>
            <el-form-item label="至下次考前，可用复习总时长是多少？（以小时为单位）" prop="study_time">
              <el-input v-model="form.study_time"></el-input>
            </el-form-item>
            <el-form-item label="是否参加过GMAT/GRE考试" prop="is_exam">
              <el-radio-group v-model="form.is_exam" @change="showExamExperienceDetails">
                <el-radio :value="1">参加过</el-radio>
                <el-radio :value="2">未参加过</el-radio>
              </el-radio-group>
            </el-form-item>

            <template v-if="form.list.length && form.is_exam == 1">
              <div class="title-info">考试记录以及对应总分</div>
              <div v-for="(exam, index) in form.list" :key="index">
                <el-row class="exam-experience-row" style="width: 100%">
                  <el-col :span="5">
                    <el-form-item label="日期">
                      <el-date-picker v-model="exam.exam_time" type="date" placeholder="选择日期"></el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="5">
                    <el-form-item label="总分">
                      <el-input v-model="exam.score" placeholder="总分"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col v-if="form.list.length > 1" :span="1" class="delete-icon-col">
                    <img src="~@/static/delete-icon.png" class="delete-icon" @click="deleteExamExperience(index)" />
                  </el-col>
                </el-row>
              </div>
            </template>
            <div class="add-exam-experience" @click="addExamExperience" v-if="form.is_exam == 1">
              <img src="~@/static/increment.png" class="increment-icon" alt="Increment Icon" />
              <span class="increment-text">再增一条</span>
            </div>
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="form.remark"> </el-input>
            </el-form-item>
          </div>
        </template>
      </el-form>
    </div>
    <el-button type="primary" @click="submitForm()" class="submitBtn">去验证手机号</el-button>
    <LoginMask></LoginMask>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from "vue";
import { useUserStore } from "@/stores/User.js";
import { useLoginStore } from "@/stores/Login.js";
const userStore = useUserStore();
const LoginStore = useLoginStore();
const form = reactive({
  username: "",
  confirm_password: "",
  password: "",
  exam_type: "",
  plan: "",
  plan_time: {
    year: "",
    month: "",
    day: "",
  },
  origin: 1,
  weixin: "",
  total_score: "",
  read_score: "",
  maths_score: "",
  study_time: "",
  is_exam: 0,
  list: [
    { 
      exam_time: "",
      score: "",
    }
  ],
  remark: "",
});
const ruleFormRef = ref();

const years = ref(Array.from({ length: 100 }, (_, i) => i + 2000));
const months = ref(Array.from({ length: 12 }, (_, i) => i + 1));
const days = ref(Array.from({ length: 31 }, (_, i) => i + 1));
const rules = ref({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  confirm_password: [{ required: true, message: "请再次输入密码", trigger: "blur" }],
  exam_type: [{ required: true, message: "请选择考试类型", trigger: "change" }],
  plan: [{ required: true, message: "请选择考试规划", trigger: "change" }],
  is_exam: [{ required: true, message: "是否参加过GMAT/GRE考试", trigger: "change" }],

  examTime: [{ required: true, message: "请选择考试时间", trigger: "change" }],
  weixin: [{ required: true, message: "请输入微信号", trigger: "blur" }],
  total_score: [{ required: true, message: "请输入英语基础分数", trigger: "blur" }],
  read_score: [{ required: true, message: "请输入阅读小分", trigger: "blur" }],
  maths_score: [{ required: true, message: "请输入高考数据成绩", trigger: "blur" }],
  study_time: [{ required: true, message: "请输入可用复习总时长", trigger: "blur" }],

  remark: [{ required: true, message: "请输入内容", trigger: "blur" }],
});

//提交代码的时候
const submitForm = () => {
  LoginStore.handleUpdate(
  [{key:'tabList',value:[{label:"验证登录手机号信息",name:"verification"}]},{key:'isActive',value:'verification'}]);
  // userStore.handleChangeVisable(false)
  // const form = ref(formRef);
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      let params = {
        confirm_password: form.confirm_password,
        exam_type: form.exam_type,
        is_exam: form.is_exam,
        list: form.list,
        maths_score: form.maths_score,
        origin: 1,
        password: form.password,
        pcode: form.pcode,
        phone: form.phone,
        plan: form.plan,
        plan_time: form.plan_time,
        read_score: form.read_score,
        remark: form.remark,
        study_time: form.study_time,
        tk: "",
        total_score: "",
        username: form.username,
        weixin: "",
        rePhone: true,
      };
      userStore.saveRegisterInfo = params;
      userStore.closeMaskOut(true);
    } else {
      console.log("error submit!!");
      return false;
    }
  });
};

const showExamPlanDetails = () => {
  if (form.plan.value == 1) {
    // form.wechatNumber.value= '';
    // form.total_score.value = '';
    // form.readingScore.value  = '';
    // form.gaokaoScor.value  = '';
    // form.studyHours.value  = '';
    // form.examExperienc.value= '';
    // form.examList.value =''
  }
};

const showExamExperienceDetails = () => {
  if (form.examExperience == 1) {
    form.list = [];
  }
};

const addExamExperience = () => {
  form.list.push({
    exam_time: "",
    score: "",
  });
  //   console.log(form.list,'form.list')
};
</script>

<style scoped lang="scss">
.register-container {
  background-color: #ecf4f2;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.container-exam {
  background-color: #eeeeee;
  padding: 10px;
  border-radius: 10px;
}

.submitBtn {
  margin-bottom: 20px;
  width: 240px;
  height: 44px;
  font-size: 16px;
  background-color: #126983;
  color: #ffffff;
  text-align: center;
  border: none;
}

.inner-container {
  width: 1000px;
  border-radius: 8px;
  background-color: #ffffff;
  margin: 20px 0;
  padding: 20px;
}

.title-info {
  font-size: 18px;
  color: #000000;
  font-weight: 600;
  margin-bottom: 20px;
}

.exam-time-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.exam-time-divider {
  text-align: center;
}

.exam-experience-row {
  display: flex;
  // justify-content: space-between;
  align-items: center;
  width: 100%;
}

.delete-icon-col {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 12px;
}

.delete-icon {
  width: 24px;
  height: 24px;
}

.add-exam-experience {
  display: flex;
  align-items: center;
  // justify-content: center;
  margin-bottom: 20px;
  cursor: pointer;
}

.increment-icon {
  width: 16px;
  height: 16px;
}

.increment-text {
  font-size: 16px;
  color: #126983;
  margin-left: 10px;
}
</style>
